package org.jeecgframework.web.material.service.impl;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.material.entity.PProductEntity;
import org.jeecgframework.web.material.entity.PartTemplateEntity;
import org.jeecgframework.web.material.entity.ProductTemplateEntity;
import org.jeecgframework.web.material.service.PartTemplateServiceI;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("partTemplateService")
@Transactional
public class PartTemplateServiceImpl extends CommonServiceImpl implements PartTemplateServiceI {

	@Override
	public void saveMain(PartTemplateEntity partTemplate) {
		TSUser user = ResourceUtil.getSessionUserName();
		partTemplate.setCompanyId(user.getCompany());
	    this.save(partTemplate);
		
		for(int i=0;i<partTemplate.getListPT().size();i++){
			ProductTemplateEntity pt = partTemplate.getListPT().get(i);
			pt.setTemplateId(partTemplate);
			pt.setCompanyId(user.getCompany());
			this.save(pt);
		}
		
	}
	
	@Override
	public void updateMain(PartTemplateEntity partTemplate) throws Exception {
		PartTemplateEntity t = this.getEntity(PartTemplateEntity.class, partTemplate.getId());
		MyBeanUtils.copyBeanNotNull2Bean(partTemplate, t);
		this.saveOrUpdate(t);
		//删除已存在的
		this.updateBySqlString("delete from product_template where template_id='"+partTemplate.getId()+"'");
		TSUser user = ResourceUtil.getSessionUserName();
		
		for(int i=0;i<partTemplate.getListPT().size();i++){
			ProductTemplateEntity pt = partTemplate.getListPT().get(i);
			pt.setTemplateId(partTemplate);
			pt.setCompanyId(user.getCompany());
			super.save(pt);
		}
		
	}
	
	@Override
	public void deleteMain(PartTemplateEntity partTemplate) {
		this.delete(partTemplate);
		//删除已存在的
		this.updateBySqlString("delete from product_template where template_id='"+partTemplate.getId()+"'");
		
	}

}